package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.DoctorPatientBean;
import com.hxkcloud.nurse.beans.HospitalStatusBean;
import com.hxkcloud.nurse.config.UrlConfig;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.views.CircleImageView;

import java.util.List;

/**
 * ProjectName: YJHCuzhong
 * PackageName: com.yjhcloud.stroke.adapter
 * Description:
 * Create by Ray.
 * Date: 2016/9/28 14:20
 */
public class MainPageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List<DoctorPatientBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ResultSelectedListener listener;
    List<HospitalStatusBean> beans;
    protected ImageLoader imageLoader;
    private int screenWidth;

    public MainPageListAdapter(Context mContext, List<DoctorPatientBean> datas, ResultSelectedListener listener, int width) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        this.screenWidth = width;
        imageLoader = ImageLoader.getInstance();
        mInflater=LayoutInflater.from(mContext);
    }

    public void setData(List<DoctorPatientBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        Holder mHolder = (Holder) holder;
        mHolder.nameTV.setText(datas.get(position).getName());
        if(datas.get(position).getAge() == 0 || "".equals(datas.get(position).getAge()+"")){
            mHolder.ageTV.setText("<1" + mContext.getResources().getString(R.string.usermanger_text_age));
        }else{
            mHolder.ageTV.setText(datas.get(position).getAge() + mContext.getResources().getString(R.string.usermanger_text_age));
        }

        DisplayImageOptions option = null;
        int age = datas.get(position).getAge();
        if (datas.get(position).getSex().equals("0")) {//male
            if (age > 49) {
                option = ImageOptionUtils.getInstance().getMan50HeadOptions();
            } else {
                option = ImageOptionUtils.getInstance().getMan20HeadOptions();
            }
        } else {//female
            if (age > 49) {
                option = ImageOptionUtils.getInstance().getWoman50HeadOptions();
            } else {
                option = ImageOptionUtils.getInstance().getWoman20HeadOptions();
            }
        }

        if(TextUtils.isEmpty(datas.get(position).getHeadImage())){
            imageLoader.displayImage(datas.get(position).getHeadImage(), mHolder.userIcon, option);
        }else{
            if(datas.get(position).getHeadImage().startsWith("image")){
                imageLoader.displayImage(UrlConfig.image_host+datas.get(position).getHeadImage(), mHolder.userIcon, option);
            }else{
                imageLoader.displayImage(datas.get(position).getHeadImage(), mHolder.userIcon, option);
            }
        }



        if("0".equals(datas.get(position).getSex())){
            mHolder.sexTV.setText(mContext.getResources().getString(R.string.male));
        }else{
            mHolder.sexTV.setText(mContext.getResources().getString(R.string.female));
        }
        mHolder.disabilityTV.setText(datas.get(position).getDisabilityName());
//        if("".equals(datas.get(position).getDisabilityName())){
//            mHolder.disabilityTV.setVisibility(View.INVISIBLE);
//        }else{
//            mHolder.disabilityTV.setVisibility(View.VISIBLE);
//        }

        if(position == datas.size()-1){
            mHolder.viewV.setVisibility(View.GONE);
        }else{
            mHolder.viewV.setVisibility(View.VISIBLE);
        }
        mHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSelected("item",position);
            }
        });


        String selfCare = datas.get(position).getDisabilityName();
        if(TextUtils.isEmpty(selfCare)){
            mHolder.disabilityTV.setVisibility(View.INVISIBLE);
        }else if(selfCare.contains(mContext.getResources().getString(R.string.selfcare_level1))){
            mHolder.disabilityTV.setText(mContext.getResources().getString(R.string.degaree_text_level1));
            mHolder.disabilityTV.setBackgroundResource(R.drawable.bg_text_line_level1);
            mHolder.disabilityTV.setTextColor(mContext.getResources().getColor(R.color.color_level1));
        }else if(selfCare.contains(mContext.getResources().getString(R.string.selfcare_level2))){
            mHolder.disabilityTV.setText(mContext.getResources().getString(R.string.degaree_text_level2));
            mHolder.disabilityTV.setBackgroundResource(R.drawable.bg_text_line_level2);
            mHolder.disabilityTV.setTextColor(mContext.getResources().getColor(R.color.color_level2));
        }else if(selfCare.contains(mContext.getResources().getString(R.string.selfcare_level3))){
            mHolder.disabilityTV.setText(mContext.getResources().getString(R.string.degaree_text_level3));
            mHolder.disabilityTV.setBackgroundResource(R.drawable.bg_text_line_level3);
            mHolder.disabilityTV.setTextColor(mContext.getResources().getColor(R.color.color_level3));
        }else if(selfCare.contains(mContext.getResources().getString(R.string.selfcare_level4))){
            mHolder.disabilityTV.setText(mContext.getResources().getString(R.string.degaree_text_level4));
            mHolder.disabilityTV.setBackgroundResource(R.drawable.bg_text_line_level4);
            mHolder.disabilityTV.setTextColor(mContext.getResources().getColor(R.color.color_level4));
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.expendlist_item, null);
        RecyclerView.ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends RecyclerView.ViewHolder {

        LinearLayout layout;
        CircleImageView userIcon;
        TextView nameTV;
        TextView ageTV;
        TextView sexTV;
        TextView disabilityTV;
        View viewV;


        public Holder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.expandlistitem_layout_item);
            userIcon = (CircleImageView) view.findViewById(R.id.expandlistitem_iv_img);
            nameTV = (TextView) view.findViewById(R.id.expandlistitem_tv_name);
            ageTV = (TextView) view.findViewById(R.id.expandlistitem_tv_age);
            sexTV = (TextView) view.findViewById(R.id.expandlistitem_tv_sex);
            disabilityTV = (TextView) view.findViewById(R.id.expandlistitem_tv_disability);
            viewV = (View) view.findViewById(R.id.expandlistitem_bottomView);

        }
    }

    public interface ResultSelectedListener {
        void onSelected(String type, int position);
    }

}
